<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <title>编辑子计划</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="csrf-token" content="{:token()}">
    <link rel="stylesheet" href="/static/frontend/lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/frontend/css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<form class="layui-form layuimini-form" autocomplete="off">
    <input type="hidden" name="id" value="{$plan['id']}">
    <div class="layui-form-item">
        <label class="layui-form-label required">名称</label>
        <div class="layui-input-block">
            <input type="text" name="name" value="{$plan['name']}" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">时间</label>
        <div class="layui-inline">
            <div class="layui-input-inline">
                <input type="text" value="{:date('Y-m-d H:i:s', $plan['start_time'])}" name="startTime" class="layui-input" id="startTime" placeholder="请选择开始时间" {$plan['status']=='已完成' ? 'disabled' : ''} lay-verify="required">
            </div>
        </div>
        <div class="layui-inline">
            <div class="layui-input-inline">
                <input type="text" value="{:date('Y-m-d H:i:s', $plan['end_time'])}" name="endTime" class="layui-input" id="endTime" placeholder="请选择结束时间" {$plan['status']=='已完成' ? 'disabled' : ''} lay-verify="required">
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">估算时间</label>
        <div class="layui-input-block">
            <input type="text" name="estimateTime" lay-verify="required|name" lay-reqtext="估算时间不能为空" placeholder="请输入时间" value="{$plan['estimate_time']}" {$plan['status']=='已完成' ? 'disabled' : ''} class="layui-input">
        </div>
    </div>
    {if $plan['finish_time'] != 0}
    <div class="layui-form-item">
        <label class="layui-form-label required">完成时间</label>
        <div class="layui-input-block">
            <input type="text" name="name" value="{:date('Y-m-d H:i:s', $plan['finish_time'])}" class="layui-input" disabled>
        </div>
    </div>
    {/if}
    <div class="layui-form-item">
        <label class="layui-form-label required">优先级</label>
        <div class="layui-input-block">
            <select name="priority" lay-verify="required" lay-search>
                <option value="低" {$plan['priority'] == '低' ? 'selected' : ''}>低</option>
                <option value="中" {$plan['priority'] == '中' ? 'selected' : ''}>中</option>
                <option value="高" {$plan['priority'] == '高' ? 'selected' : ''}>高</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">详情描述</label>
        <div class="layui-input-block">
            <textarea name="desc" placeholder="请输入内容" lay-verify="desc" class="layui-textarea">{$plan['desc']}</textarea>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
        </div>
    </div>
</form>
<script src="/static/frontend/lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'layer', 'laydate'], function () {
        var form = layui.form,
            layer = layui.layer,
            $ = layui.$,
            laydate = layui.laydate;

        //开始时间选择器
        laydate.render({
            elem: '#startTime'
            ,type: 'datetime'
            ,trigger: 'click'
        });

        // 结束时间选择器
        laydate.render({
            elem: '#endTime'
            ,type: 'datetime'
            ,trigger: 'click'
        });

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            $.ajaxSetup({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                }
            });
            $.post('/frontend/plan/sub-plan/update', data.field, function (res) {
                if (res.code == 0) {
                    // 修改成功
                    layer.msg(res.msg, {
                        icon: 1,
                        time: 1000
                    },function () {
                        // 重新加载父页面
                        parent.location.reload();
                    })
                } else if (res.code == 302) {
                    // 错误跳转
                    location.href = '/frontend/error/' + res.msg;
                } else {
                    // 修改失败
                    layer.msg(res.msg, {icon: 2});
                }
            }, 'json');
            return false;
        });

        // 自定义验证规则
        form.verify({
            name: function (value, item) {
                if (value.length > 20) {
                    return '标题不能超过20个字符';
                }
            },
            desc: function (value, item) {
                if (value.length > 255) {
                    return '详情描述不能超过255个字符';
                }
            }
        })

    });
</script>
</body>
</html>